package com.yinhai.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yinhai.activity.R;
import com.yinhai.constant.Contact;
import com.yinhai.model.ContactsUser;

import java.util.List;

public class DialInContactAdapter extends BaseAdapter {
    
    private List<ContactsUser> list;
    
    private LayoutInflater inflater;
    
    private String dialedNum;
    Context context;
    public DialInContactAdapter(Context context, List<ContactsUser> list, String dialNum) {
        this.list = list;
        this.context= context;
        this.dialedNum = dialNum;
        this.inflater = LayoutInflater.from(context);
    }
    
    public void setDialNum(String dialNum) {
        this.dialedNum = dialNum;
    }
    
    @Override
    public int getCount() {
        return list.size();
    }
    
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.dial_list_item_incontact, null);
            holder = new ViewHolder();
            holder.nameAndGroup = (TextView)convertView.findViewById(R.id.dial_item_name_contact);
            holder.number = (TextView)convertView.findViewById(R.id.dial_item_tel_contact);
            holder. imgHeader = (ImageView) convertView.findViewById(R.id.dial_head_pic_contact);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        Glide.with(context).load("").placeholder(R.drawable.tel_photo).into(holder.imgHeader);

        ContactsUser contactsUser = list.get(position);
        if (dialedNum != null && dialedNum.length() > 0) {
            // 名字变色
            String userName = contactsUser.getName();
            String changeType = contactsUser.getChangeType();
            String groupName = contactsUser.getGroupName();
            if (groupName != null) {
                userName = userName + " " + groupName;
            }
            
            if (Contact.MATCH_NAME.equals(changeType) || Contact.MATCH_ALL.equals(changeType)) {
                SpannableStringBuilder style = new SpannableStringBuilder(userName);
                style.setSpan(new ForegroundColorSpan(Color.parseColor("#009900")),
                    contactsUser.getStart(),
                    contactsUser.getEnd(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.nameAndGroup.setText(style);
            } else {
                holder.nameAndGroup.setText(userName);
            }
            holder.nameAndGroup.setTag(contactsUser.getName());
            // 电话号码变色
            String phoneNum = contactsUser.getTelPhone();
            if (Contact.MATHCH_TEL.equals(changeType) || Contact.MATCH_ALL.equals(changeType)) {
                SpannableStringBuilder numStyle = new SpannableStringBuilder(phoneNum);
                if ("".equals(phoneNum) || null == phoneNum) {
                    contactsUser.setStart(0);
                    contactsUser.setEnd(0);
                }
                numStyle.setSpan(new ForegroundColorSpan(Color.parseColor("#009900")),
                    contactsUser.getStart(),
                    contactsUser.getEnd(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.number.setText(numStyle);
            } else {
                holder.number.setText(phoneNum);
            }
            
        }
        if (holder.nameAndGroup.getText() == null || "".equals(holder.nameAndGroup.getText())) {
            holder.nameAndGroup.setText("未知联系人");
        }
        // 设置号码
        holder.number.setTag(contactsUser.getTelPhone());
        
        return convertView;
    }
    
    public static class ViewHolder {
        TextView nameAndGroup;
        
        TextView number;

        ImageView imgHeader;
        
        public String getTelPhone() {
            return (String)number.getTag();
        }
        
        public String getName() {
            return (String)nameAndGroup.getTag();
        }
    }
    
}
